package com.sfh.tourist;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.sfh.lib.ui.DialogBuilder;
import com.sfh.lib.ui.MVVMActivity;
import com.tianditu.android.maps.GeoPoint;
import com.tianditu.android.maps.MapController;
import com.tianditu.android.maps.MapView;
import com.tianditu.android.maps.MyLocationOverlay;
import com.tianditu.android.maps.TErrorCode;
import com.tianditu.android.maps.TGeoAddress;
import com.tianditu.android.maps.TGeoDecode;
import com.tianditu.android.maps.overlay.MarkerOverlay;

import javax.microedition.khronos.opengles.GL10;

public class AmapActivity extends MVVMActivity {

    MapView mMapView;
    TextView tv_content;
    MapController mMapController;
    MyLocationOverlay mLocation;
    boolean isLocation = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_activity);

        findViewById(R.id.tv_back).setOnClickListener(view -> finish());
        tv_content = findViewById(R.id.tv_content);

        // 地图视图
        mMapView = (MapView) findViewById(R.id.main_mapview);
        mMapView.setLogoPos(MapView.LOGO_LEFT_TOP);
        mMapView.setBuiltInZoomControls(true);
        //得到mMapView的控制权,可以用它控制和驱动平移和缩放
        mMapController = mMapView.getController();
        //设置地图zoom级别
        mMapController.setZoom(12);
        mLocation = new MyLocationOverlay(this, mMapView) {

            @Override
            public void onLocationChanged(Location location) {
                super.onLocationChanged(location);
                if (isLocation) {
                    return;
                }
                isLocation = true;
                GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));
                //从location对象中获取经纬度信息，地址描述信息，
                mMapController.setCenter(point);
                addMarkerOverlay(point);
                search(point);
            }
        };
        mMapView.addOverlay(mLocation);

        findViewById(R.id.tv_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mPoint == null || mAddress == null) {
                    return;
                }
                double dLon = ((double) mPoint.getLongitudeE6()) / 1E6;
                double dLat = ((double) mPoint.getLatitudeE6()) / 1E6;

                DialogBuilder dialogBuilder = new DialogBuilder();
                dialogBuilder.setTitle("当前定位信息");
                dialogBuilder.setMessage("经纬值：" + dLon+ "," +dLat + "\n位置描述：" + mAddress.getFullName());
                dialogBuilder.setOkListener(new DialogBuilder.DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogBuilder.DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Intent intent = new Intent();


                        intent.putExtra("Latitude",dLat);
                        intent.putExtra("Longitude", dLon);
                        intent.putExtra("Address", mAddress.getFullName());
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                    }
                });
                showDialog(dialogBuilder);

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mLocation.enableMyLocation();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mLocation.disableMyLocation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
    }

    GeoPoint mPoint;
    TGeoAddress mAddress;
    TGeoDecode mGeoDecode;
    MarkerOverlay overlay;

    private void search(final GeoPoint point) {
        if (mGeoDecode == null) {
            mGeoDecode = new TGeoDecode(new TGeoDecode.OnGeoResultListener() {
                @Override
                public void onGeoDecodeResult(TGeoAddress addr, int errCode) {
                    if (errCode != TErrorCode.OK) {
                        return;
                    }
                    if (addr == null) {
                        return;
                    }
                    mPoint = point;
                    mAddress = addr;
                    tv_content.setText("经纬值：" +getGeoPoint(mPoint) + "\n位置描述：" + addr.getFullName());
                }
            });
        }
        mGeoDecode.search(point);
    }
    private static String getGeoPoint(GeoPoint geoPoint) {
        double dLon = ((double) geoPoint.getLongitudeE6()) / 1E6;
        double dLat = ((double) geoPoint.getLatitudeE6()) / 1E6;
        String string = String.format("lon = %.6f, lat = %.6f", dLon, dLat);
        return string;
    }
    private void addMarkerOverlay(GeoPoint point) {
        if (overlay == null) {
            overlay = new MarkerOverlay();
            overlay.setDraggable(true);
            overlay.setIcon(ContextCompat.getDrawable(AmapActivity.this, R.mipmap.order_location_green));
            overlay.setDragListener(new MarkerOverlay.OnMarkerDragListener() {
                @Override
                public void onMarkerDragStart(MarkerOverlay markerOverlay) {

                }

                @Override
                public void onMarkerDrag(MarkerOverlay markerOverlay) {

                }

                @Override
                public void onMarkerDragEnd(MarkerOverlay markerOverlay) {
                    GeoPoint location = markerOverlay.getPosition();
                    search(location);
                }
            });
            mMapView.addOverlay(overlay);
        }
        overlay.setPosition(point);
    }
}
